﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Collections.ObjectModel;
using Sebarf.Diagnostics.Interfaces;
using Sebarf.Security.Interfaces;
using Sebarf.Services.Interfaces;

#endregion

namespace Sebarf.Security.Authentication {
	/// <summary>
	/// Description of the class
	/// </summary>
	public class AuthenticationManagementService : Service, IAuthenticationManagementService {
		#region Public Properties

		public ObservableCollection<IAuthenticator> Authenticators { get; set; }

		public IIdentitity CurrentIdentity { get; private set; }

		#endregion

		#region Public Methods

		public AuthenticationManagementService() {
			Authenticators = new ObservableCollection<IAuthenticator>();
		}

		#endregion

		#region IAuthenticationManagementService Members

		public bool Authenticate(DoAuthenticationRequest request) {
			CurrentIdentity = null;

			foreach (IAuthenticator authenticator in Authenticators) {
				IIdentitity identity = null;
				try {
					identity = authenticator.Authenticate(request);
				}
				catch (Exception ex) {
					Logger.WriteDebug("authenticator returns:" + ex.Message);
					identity = null;
				}
				if (identity != null && identity.IsAuthenticated) {
					CurrentIdentity = identity;
				}
				break;
			}
			return IsAuthenticated();
		}

		public bool IsAuthenticated() {
			return CurrentIdentity != null && CurrentIdentity.IsAuthenticated;
		}

		public IIdentitity GetIdentity() {
			return CurrentIdentity;
		}

		#endregion
	}
}